#include "MainWindow.h"
#include <QToolBar>
#include <QAction>
#include <QComboBox>
#include <QLineEdit>
#include <QIntValidator>
#include <QApplication>

MainWindow::MainWindow(QWidget *) {
    m_view = new BarWidget(this);
    setCentralWidget(m_view);

    m_thread = new SortThread(this);
    connect(m_thread, &SortThread::frameReady, this, &MainWindow::updateFrame);
    m_thread->start();

    /* 工具栏 */
    auto *tb = addToolBar("ctrl");
    m_play = tb->addAction("▶︎");
    m_speedBox = new QComboBox;
    m_shuffleBox = new QComboBox;
    m_algoBox = new QComboBox;
    m_sizeEdit = new QLineEdit("128");
    m_sizeEdit->setValidator(new QIntValidator(32, 16384, this));

    for (double s: {0.2, 0.5, 1.0, 1.5, 2.0, 5.0})
        m_speedBox->addItem(QString("%1x").arg(s), s);

    m_shuffleBox->addItems({"几乎有序", "逆序", "随机"});
    m_algoBox->addItems({
        "冒泡排序", "简单插入排序", "选择排序",
        "二分插入排序", "希尔排序",
        "归并排序", "单边循环快排", "双基准点快排",
        "堆排序",
        "计数排序", "桶排序", "LSD基数排序(10进制)", "TimSort",
        "std::sort", "并行归并排序", "并行快速排序", "并行TimSort", "双调排序"
    });

    tb->addWidget(m_speedBox);
    tb->addWidget(m_shuffleBox);
    tb->addWidget(m_algoBox);
    tb->addWidget(m_sizeEdit);

    /* 信号槽 */
    connect(m_play, &QAction::triggered, this, &MainWindow::playPause);
    connect(m_speedBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &MainWindow::speedChanged);
    connect(m_shuffleBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &MainWindow::shuffleChanged);
    connect(m_algoBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &MainWindow::algoChanged);
    connect(m_sizeEdit, &QLineEdit::editingFinished,
            this, &MainWindow::sizeEdited);
    connect(m_thread, &SortThread::frameReady,
            m_view, &BarWidget::setData);

    /* 默认配置 */
    m_cfg.N = 128;
    m_cfg.speedMult = 1.0;
    m_speedBox->setCurrentIndex(2);
    m_thread->applyConfig(m_cfg);
}

MainWindow::~MainWindow() {
    m_thread->stop();
    m_thread->wait();
}

/* ▶︎/⏸ */
void MainWindow::playPause() {
    m_thread->togglePause();
    m_play->setText(m_play->text() == "▶︎" ? "⏸" : "▶︎");
}

/* 倍速：动态生效，不暂停 */
void MainWindow::speedChanged(int) {
    double s = m_speedBox->currentData().toDouble();
    m_cfg.speedMult = s;
    m_thread->updateSpeed(s); // ★ 即时改速
}

/* 改算法 → 暂停 */
void MainWindow::algoChanged(int idx) {
    m_cfg.algo = static_cast<Algo>(idx);
    m_thread->applyConfig(m_cfg);
    m_play->setText("▶︎");
}

/* 改数量 → 暂停 */
void MainWindow::sizeEdited() {
    m_cfg.N = m_sizeEdit->text().toInt();
    m_thread->applyConfig(m_cfg);
    m_play->setText("▶︎");
}

/* 打乱 → 暂停+显示乱序 */
void MainWindow::shuffleChanged(int idx) {
    m_cfg.shuffle = static_cast<ShuffleMode>(idx);
    m_thread->applyConfig(m_cfg);
    m_thread->forceShuffle();
    m_play->setText("▶︎");
}

/* —— New ——  接收三参数并转给 BarWidget */
void MainWindow::updateFrame(const QVector<uint32_t> &data,
                             const QVector<int> &tag,
                             SortStats st) {
    m_view->setData(data, tag, st);
    setWindowTitle(QString("MultiSortVis  |  %1 elements").arg(data.size()));
}
